package com.kronos.gui;

import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.kronos.audio.SoundStream;
import com.kronos.game.KronosGame;

public class ImageFontButton extends Drawable {

	private Texture button;
	private TextureRegion region, hoverRegion;

	private float mouseX = 0.0F, mouseY = 0.0F;

	// Width and height of button region
	public int imgWidth = 77;
	public int imgHeight = 33;
	private float SCALE = 1.2f;

	private BitmapFont font;

	private String str;

	private Vector3 vec;
	private OrthographicCamera cam;

	private boolean hovering = false;
	private boolean lastMouseOver = false;

	public ImageFontButton(String str, int x, int y, OrthographicCamera cam) {
		this.x = x;
		this.y = y;
		this.str = str;

		this.button = KronosGame.manager.getTexture("gui.png");
		this.region = new TextureRegion(button, 14, 66, this.imgWidth, this.imgHeight);
		this.hoverRegion = new TextureRegion(button, 101, 66, this.imgWidth, this.imgHeight);

		this.vec = new Vector3();
		this.cam = cam;

		this.font = new BitmapFont(); 
	}

	public void setString(String str) {
		this.str = str;
	}

	private boolean inBounds() { 
		float mouseX = this.mouseX;
		float mouseY = this.mouseY;

		vec.set(mouseX, mouseY, 0);
		cam.unproject(vec);  
		// (0,0) Starts at bottom left corner
		return ((vec.x >= x && vec.x <= x + region.getRegionWidth() * SCALE) && (vec.y <= y
				+ region.getRegionHeight() * SCALE && vec.y >= y));
	}

	public void update() { }

	public void render(SpriteBatch sb) {
		sb.begin();

		sb.setColor(1.0f, 1.0f, 1.0f, 0.2f);
		if (hovering) {
			sb.draw(hoverRegion, x, y, imgWidth * SCALE, imgHeight * SCALE);
		} else {
			sb.draw(region, x, y, imgWidth * SCALE, imgHeight * SCALE);
		}

		if (str != null) {
			font.draw(sb, str, x + region.getRegionWidth() * SCALE / 2 - font.getBounds(str).width / 2, 
					y + region.getRegionHeight() * SCALE / 2 + font.getBounds(str).height / 2);
		}

		sb.end();
	}

	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		if(keycode == Keys.ENTER) {
				this.changedState = true;
		}
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		this.mouseX = screenX;
		this.mouseY = screenY;

		if (!disabled) {
			if (inBounds() & !lastMouseOver) {
				SoundStream.playSound(1);
				lastMouseOver = true;
			} else if (!inBounds()) {
				lastMouseOver = false;
			}

			if (inBounds()) {
				hovering = true;
			} else {
				hovering = false;
			}
		}
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		if(inBounds()) {
			this.changedState = true;
		}
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(float x, float y, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean tap(float x, float y, int count, int button) {
		this.mouseX = x;
		this.mouseY = y;

		return false;
	}

	@Override
	public boolean longPress(float x, float y) {
		System.out.println(x +"," + y);
		return false;
	}

	@Override
	public boolean fling(float velocityX, float velocityY, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean pan(float x, float y, float deltaX, float deltaY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean panStop(float x, float y, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean zoom(float initialDistance, float distance) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2,
			Vector2 pointer1, Vector2 pointer2) {
		// TODO Auto-generated method stub
		return false;
	}
}
